package com.masterclient.app.modules.homepage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.eoner.baselibrary.bean.firstpager.RecommendPagerBean;
import com.eoner.baselibrary.bean.groupbuy.GroupBuyDetailBean;
import com.eoner.baselibrary.utils.DensityUtil;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.RoundedCornersTransform;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HotProductAdapter extends RecyclerView.Adapter {


    private Context context;

    private List<RecommendPagerBean.ShProduct> shProducts;


    public HotProductAdapter(Context context, List<RecommendPagerBean.ShProduct> shProducts) {
        this.context = context;
        this.shProducts = shProducts;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_hot_product_item,parent,false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,  int position) {

        ViewHolder viewHolder = (ViewHolder) holder;
        RecommendPagerBean.ShProduct shProduct = shProducts.get(position);
        //viewHolder.price.setText("￥" + shProduct.getSh_show_price());
        Utils.set_price_style(viewHolder.price,"￥" + shProduct.getSh_show_price());
        Utils.setDin(viewHolder.price,context);
        Utils.setDin(viewHolder.tvMoneyEarn,context);
        viewHolder.tvMoneyEarn.setText("赚"+shProduct.getSh_partner_commission());
        viewHolder.name.setText(shProduct.getSh_name());
//        App.picasso.load(shProduct.getSh_image())
//                //.resize(ScreenUtils.dp2px(180),ScreenUtils.dp2px(120))
//                .placeholder(R.mipmap.defaults)
//                .into(viewHolder.image);
        //GlideUtil.displayRoundImage(context,shProduct.getSh_image(),viewHolder.image,7,R.mipmap.defaults);

        if (TextUtils.isEmpty(shProduct.getSh_partner_commission())||
                Double.parseDouble(shProduct.getSh_partner_commission())==0){
            viewHolder.tvMoneyEarn.setVisibility(View.GONE);
        }else {
            viewHolder.tvMoneyEarn.setVisibility(View.VISIBLE);
        }

        RoundedCornersTransform transform = new RoundedCornersTransform(context, DensityUtil.dip2px(7));
        transform.setNeedCorner(true, true, false, false);
        RequestOptions options = new RequestOptions().placeholder(R.mipmap.defaults).transform(transform);
        Glide.with(context).asBitmap().load(shProduct.getSh_image()).apply(options).into(viewHolder.image);

        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                        .withString("id", shProduct.getSh_id())
                        .navigation();
            }
        });

        if(Long.valueOf(shProduct.getSh_stock()) > 0){
            viewHolder.sell_out_layout.setVisibility(View.INVISIBLE);
        }else{
            viewHolder.sell_out_layout.setVisibility(View.VISIBLE);
        }


        if (shProduct.getSh_label() != null && !TextUtils.isEmpty(shProduct.getSh_label().getSh_icon_height())) {
            GroupBuyDetailBean.DataBean.ShRemProductBean.ShLabelBean labelBean = shProduct.getSh_label();
            GlideUtil.displayImage(context,labelBean.getSh_icon(),viewHolder.iv_activity,R.mipmap.defaults_1);
            viewHolder.iv_activity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams paramsActivity = (RelativeLayout.LayoutParams) viewHolder.iv_activity.getLayoutParams();
            paramsActivity.height = ScreenUtils.dp2px(Integer.valueOf(labelBean.getSh_icon_height()) / 2);
            paramsActivity.width = ScreenUtils.dp2px(Integer.valueOf(labelBean.getSh_icon_width()) / 2);
            switch (labelBean.getSh_location()) {
                case "left_top":
                    paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    break;
                case "right_top":
                    paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    break;
                case "left_bottom":
                    paramsActivity.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.image);
                    paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    paramsActivity.setMargins(ScreenUtils.dp2px(7), 0, 0, ScreenUtils.dp2px(7));
                    break;
                case "right_bottom":
                    paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    paramsActivity.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.image);
                    paramsActivity.setMargins(0, 0, ScreenUtils.dp2px(7), ScreenUtils.dp2px(7));
                    break;
            }
            viewHolder.iv_activity.setLayoutParams(paramsActivity);
        }else{
            viewHolder.iv_activity.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return shProducts.size();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
        @BindView(R.id.name)
        public TextView name;
        @BindView(R.id.price)
        public TextView price;
        @BindView(R.id.image)
        public ImageView image;
        @BindView(R.id.iv_activity)
        public ImageView iv_activity;
        @BindView(R.id.tv_money_earn)
        public TextView tvMoneyEarn;
        @BindView(R.id.sell_out_layout)
        public View sell_out_layout;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }


}

